
const request = require('../../../../utils/request');

Page({
    data: {
        foods: [],
        shop: {},   //商家数据
        allPrice: 0,
        note: '',
        advData: {},     //预定数据,
        type: '' ,   //判断是哪个订单方式
        advPrice: 0,
        address_id:0,
        address_name:'',
        address_tel:'',
        address:'',
        penny: 0,    //预定里的定金
        minConsumption: false,    //是否是房桌最低消费
        selectRoom: []  //选择的房间
    },
    onLoad: function(e) {    
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        console.log(prevPage)
        let price = 0;
        let foods = '';
        let allPrice = 0;
        //是否再来一单
        if(e.status == 'angin'){
            foods = prevPage.data.foods;
            foods.forEach(val => {
                price += val.count * val.discount
            })
            request.HttpRequst(true,'program/shops',false,'',{"id": e.id },"GET",false,res => {
                allPrice = Math.floor(price*100)/100 + Math.floor(res.data.shopinfo.spend*100)/100;
                console.log(res)
                this.setData({
                    foods:foods,
                    shop:res.data,
                    type:'外卖',
                    allPrice: Math.floor(allPrice*100)/100
                })
            })
            
        }else{
            
            foods = prevPage.__data__.foods;
            foods.forEach(val => {
                
                if(val.specPrice){
                    price += val.count * (val.discount * val.price + val.specPrice) * prevPage.__data__.allData.selectRoom.length
                }else{
                    price += val.count * val.discount * val.price * prevPage.__data__.allData.selectRoom.length
                }
            })
            let advPrice = 0;
            console.log(Math.floor(price*100)/100 * prevPage.__data__.allData.selectRoom.length)
            if(prevPage.__data__.allData.min_consumption > Math.floor(price*100)/100){
                advPrice = prevPage.__data__.allData.min_consumption;
                this.setData({
                    minConsumption: true
                })
            }else{
                advPrice = Math.floor(price*100)/100
            }
            //let advPrice = prevPage.__data__.allData.min_consumption > Math.floor(price*100)/100 ? prevPage.__data__.allData.min_consumption : Math.floor(price*100)/100;
            allPrice = Math.floor(price*100)/100 + Math.floor(prevPage.__data__.goods.shopinfo.spend*100)/100;
            //选择的房间
            let room = [];
            prevPage.__data__.allData.selectRoom.forEach( val => {
                console.log(val)
                room.push(val.split(','))
            })
            this.setData({
                foods: foods,
                shop: prevPage.__data__.goods,
                advData: prevPage.__data__.allData,
                type: prevPage.__data__.type,
                allPrice: Math.floor(allPrice*100)/100,
                advPrice: Math.floor(advPrice*100)/100,
                selectRoom: room
            })
        }
        
        //let foods = prevPage.__data__.foods;
        
        
    },
    onShow: function() {
        //获取地址
        request.HttpRequst(true, 'user/allAddress', false, '', { "token": wx.getStorageSync("sessionId")}, "post", false,res =>{
            console.log(res)
            if(res.code==200){
                res.data.forEach( val => {
                    if(val.pretermission == 1){
                        this.setData({
                            address_id: val.id,
                            address_name:val.name,
                            address_tel:val.mobile,
                            address:val.address+' '+val.address_detail
                        })
                    }
                })
            }else{
              return
            }
        })
    },
    onReady: function(){
        wx.setNavigationBarTitle({
            title: '订单'
          })
    },
    putOrder() {
        // if(this.data.address == ''){
        //     console.log("请填写地址!")
        //     wx.showModal({
        //         title: '提示',
        //         content: '请填写地址!',
        //         showCancel: false,
        //     })
        //     return false
        // }
        console.log(this.data.note);
        let cart = [];
        let advCart = [];
        let advAddDish = {};    //预定加菜
        let dish = [];
        let number = 0;
        this.data.foods.forEach(res => {
            if(res.spec){
                let arr = [];
                res.spec.forEach( val => {
                    arr.push(val.spec_name+"|"+val.spec_price)
                })
                res.specName =  (arr.join(',')).replace(/\s*/g,"");
            }else{
                res.specName = '';
            }
            let a = JSON.parse(`{"dishes_id": "${res.id}","dishes_name": "${res.dishes_name}","goods_number": "${res.count}","price": "${res.price}","sell_price": "${res.discount}","is_attr": "${res.is_attr}","spec": "${res.specName}"}`)
            dish.push(a);
            number += res.count;
            
        })
        let b = JSON.parse(`{"shop_id":"${this.data.shop.id}","address_id":"${this.data.address_id}","phone":"${this.data.shop.shop_phone}","top_price":"${this.data.allPrice}","note":"${this.data.note}","name":"hauge","token":"${wx.getStorageSync('sessionId')}"}`);
        b.dish = dish
        cart.push(b)
        console.log(cart)

        //预定订单数据处理
        if(this.data.type == '预定'){
            let adv = this.data.advData;
            console.log(adv);
            adv.top_price = this.data.advPrice;
            adv.note = this.data.note;
            adv.token = wx.getStorageSync('sessionId');
            adv.dish = dish;
            advCart.push(adv);
            //加菜部分
            advAddDish.dish = dish;
            advAddDish.order_id = adv.order_id;
            advAddDish.desk_room_id = adv.desk_room_id
            if(!wx.getStorageSync('sessionId')){
                wx.switchTab({
                    url: '../../../my/my'
                })
            }else{
                if(adv.status == 'addDish'){
                    request.HttpRequst(true,'addDishes',2,'',{"cart": advAddDish , 'token':adv.token},"POST",false,res => {
                        console.log(res);
                        if(res.code == 200){
                            wx.removeStorageSync('foods');
                            let id = res.data;
                            wx.showModal({
                                title: '提示',
                                content: res.info,
                                showCancel: false,
                                success: function() {
                                    wx.navigateTo({
                                        url: '/pages/my/order/detail/detail?id='+id+'&order_type=1'
                                    })
                                }
                            })
                        }
                        // let penny = res.data.money;
                        // if(res.code==200){
                        //     wx.navigateTo({
                        //         url: '../../pay/pay?order_id='+res.data.order_id+'&price='+penny+'&type='+this.data.type
                        //     })
                        // }else{
                        //     wx.showModal({
                        //         title: '提示',
                        //         content: res.info,
                        //         showCancel: false,
                        //     })
                        // }
                    })
                }else{
                    request.HttpRequst(true,'program/RaddOrder',2,'',{"cart": advCart },"POST",false,res => {
                        console.log(res);
                        let penny = res.data.money;
                        if(res.code==200){
                            wx.navigateTo({
                                url: '../../pay/pay?order_id='+res.data.order_id+'&price='+penny+'&type='+this.data.type
                            })
                        }else{
                            wx.showModal({
                                title: '提示',
                                content: res.info,
                                showCancel: false,
                            })
                        }
                    })
                }
                
            }
              
        }else{
            if(!wx.getStorageSync('sessionId')){
                console.log("session不存在")
                wx.switchTab({
                    url: '../../../my/my'
                })
            }else{
                request.HttpRequst(true,'program/TaddOrder',2,'',{"cart": cart },"POST",false,res => {
                    console.log(res)
                    if(res.code==200){
                        wx.navigateTo({
                            url: '../../pay/pay?order_id='+res.data.order_id+'&price='+cart[0].top_price
                        })
                    }else{
                        wx.showModal({
                            title: '提示',
                            content: res.info,
                            showCancel: false,
                        })
                    }
                })
            }
            
        }
        
    },
    bindText(e) {
        
        this.setData({
            note: e.detail.value
        })
        console.log(this.data.note);
    },

    //跳转地址
    goSelectAttr() {
        wx.navigateTo({
            url: '../../../my/site/address/address'
        })
    }
})